home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / STOPWTCH.AML < prev    next >
Text File  |  1996-07-17  |  4KB  |  153 lines

  1. //--------------------------------------------------------------------
  2. // STOPWTCH.AML
  3. // Stopwatch Timer and Command Timer, (C) 1993-1996 by nuText Systems
  4. //
  5. // (see Stopwtch.dox for user help)
  6. //
  7. // This macro displays a modeless stopwatch dialog box with start/stop
  8. // and reset buttons, and real-timer clock.
  9. //
  10. // Pressing the Command button allows you measure how long it takes to
  11. // execute an editor keyboard command. The command will be executed and
  12. // the time will be displayed in the stopwatch dialog box.
  13. //
  14. // The stopwatch dialog box is 'modeless', allowing windows to be
  15. // switched while the stopwatch is running.
  16. //
  17. // Usage:
  18. //
  19. // Select this macro from the Macro List (on the Macro menu), or run it
  20. // from the macro picklist <shift f12>.
  21. //--------------------------------------------------------------------
  22.  
  23. include bootpath "define.aml"
  24.  
  25. // colors
  26. constant watch_timer_color = color brightred on black
  27. constant watch_clock_color = color black on gray
  28.  
  29. // timer delays
  30. constant timer_delay = 30
  31. constant clock_delay = 1000
  32.  
  33. variable basetime, starttime, dlgwin
  34.  
  35. // timer id's
  36. variable timerid, clockid, commandid
  37.  
  38. // get the current time in hundreths of a second
  39. // since the beginning of the month
  40. private function gethundredths
  41.   time = getrawtime
  42.   return time [16:2] + time [14:2] * 100 +
  43.          time [12:2] * 6000 + time [10:2] * 360000 +
  44.          time [7:2] * 8640000
  45. end
  46.  
  47. // timer function to update the timer display
  48. function tick (zero)
  49.   oldwindow = gotowindow dlgwin
  50.   uu = if? zero 0 gethundredths - starttime + basetime
  51.   uu = uu mod 8640000
  52.   hh = uu / 360000
  53.   uu = uu mod 360000
  54.   mm = uu / 6000
  55.   uu = uu mod 6000
  56.   writestr ' ' + hh : 2 : '0' + ':' + mm : 2 : '0'+ ':' +
  57.            (uu / 100) : 2 : '0' + ' ' + (uu mod 100) : 2 : '0' + ' '
  58.            watch_timer_color  3 2
  59.   gotowindow oldwindow
  60. end
  61.  
  62. // timer function to update the real-time clock
  63. function clocktick
  64.   oldwindow = gotowindow dlgwin
  65.   writestr ' ' + (gettime -1) watch_clock_color  33 2
  66.   gotowindow oldwindow
  67. end
  68.  
  69. // start/stop the timer
  70. function startstop
  71.   if timer? timerid then
  72.     destroytimer timerid
  73.     basetime = gethundredths - starttime + basetime
  74.   else
  75.     starttime = gethundredths
  76.     timerid = setrepeat '' timer_delay (getcurrobj) "tick"
  77.   end
  78. end
  79.  
  80. // reset the timer
  81. function reset
  82.   // stop the timer if it's running
  83.   if timer? timerid then
  84.     startstop
  85.   end
  86.   basetime = 0
  87.   tick 1
  88. end
  89.  
  90. macrofile = arg 1
  91.  
  92. // called by Lib.x when a key is entered in the dialog box
  93. function ondialog (keycode)
  94.   // macro help
  95.   if keycode == <f1> then
  96.     helpmacro macrofile
  97.   end
  98. end
  99.  
  100. // display the stopwatch dialog box
  101. // and start the real-time clock
  102. function timerbox (zero)
  103.   dlgwin = dialog "Stopwatch" 46 5 "c" (getcurrobj)
  104.   tick zero
  105.   clocktick
  106.   clockid = setrepeat '' clock_delay (getcurrobj) "clocktick"
  107.   button "&Start/Stop"      3  4 13  whenenter "startstop"
  108.   button "&Reset"          19  2 11  whenenter "reset"
  109.   button "&Command"        19  4 11  whenenter "timecmd"
  110.   button "E&xit"           33  4 11
  111.   showdialog
  112. end
  113.  
  114. variable startcommand
  115.  
  116. function endcommand
  117.   starttime = gethundredths
  118.   basetime = starttime - startcommand
  119.   timerbox
  120. end
  121.  
  122. // time an editor keyboard command
  123. function timecmd
  124.   sendobject (getwinobj) "close" "hide"
  125.   queue "eval" "set 'kc' (getkey) '" + getcurrobj + "' queue <esc>"
  126.   shortbox  "Enter a keyboard command to time, or <Esc> to exit."
  127.   keycode = _kc
  128.   if keycode <> <esc> and keycode <> <lbutton> and
  129.      keycode <> <rbutton> then
  130.     queue (geteventname keycode)
  131.     // timers events execute with the lowest priority
  132.     commandid = settimer '' 0 (getcurrobj) "endcommand"
  133.     startcommand = gethundredths
  134.   else
  135.     timerbox 1
  136.   end
  137. end
  138.  
  139. // called by Lib.x when the dialog box is closed
  140. function onclosedlg (title)
  141.   reset
  142.   destroytimer clockid
  143.   if title <> "hide" then
  144.     destroyobject
  145.   end
  146. end
  147.  
  148. // keep this macro resident at startup
  149. resident ON
  150.  
  151. // display the dialog box
  152. timerbox 1
  153.